package com.framework.web.bus.controller;


import com.framework.core.domain.AjaxJson;
import com.framework.core.easyui.domain.DataGridModel;
import com.framework.core.easyui.util.PageUtil;
import com.framework.core.util.MyBeanUtils;
import com.framework.web.bus.domain.GoodsType;
import com.framework.web.bus.service.GoodsTypeService;
import com.framework.web.common.mappings.SysUrlMappings;
import com.framework.web.sys.domain.Organization;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
* @Title: Controller
* @Description: 控制层
* @author laowang
* @date 2016-09-11
* @version V1.0
*
*/
@Controller
@RequestMapping("/goodsType")
public class GoodsTypeController {

private Logger logger = LoggerFactory.getLogger(this.getClass());


/**
* Spring注入资源信息模块service
*/
@Autowired
private GoodsTypeService goodsTypeService;


/**
*
* 分页
* @param dgm
* @param goodsType
* @return
* @throws Exception
*/
@RequestMapping(value = "/list", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> list(DataGridModel dgm, GoodsType goodsType, HttpServletRequest request) throws Exception {
    PageRequest pr = DataGridModel.getPageRequest(dgm);
    return PageUtil.getPageMap(goodsTypeService.getPageList(goodsType, pr));
}

/**
*  请求新增或修改页面
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/toSaveAndUpdate/{id}", method = RequestMethod.GET)
public ModelAndView toSaveAndUpdate(@PathVariable("id") String id)
throws Exception {
    ModelAndView modelAndView = new ModelAndView("bus/goodstype/goodsTypeForm");
    GoodsType goodsType = new GoodsType();
    if (StringUtils.isNotEmpty(id) && !"0".equals(id)) {
        goodsType = goodsTypeService.findById(id);
    } else {

    }
    modelAndView.addObject("goodsType", goodsType);
    return modelAndView;
}


/**
* 保存信息
*
* @param goodsType  信息实体
* @return
* @throws Exception
*/
@RequestMapping(value = "/saveAndUpdate", method = RequestMethod.POST)
@ResponseBody
public AjaxJson saveAndUpdate(HttpServletRequest request, HttpServletResponse response, GoodsType goodsType) throws Exception {
    AjaxJson j = new AjaxJson();
    if (StringUtils.isNotEmpty(goodsType.getId())) {//更新
        GoodsType r = goodsTypeService.findById(goodsType.getId());
        MyBeanUtils.copyBeanNotNull2Bean(goodsType, r);
        goodsType = r;
    }else {

    }
    goodsTypeService.save(goodsType);
    j.setMsg("保存成功");
    return j;
}

/**
* 根据id字符串删除数据信息
*
* @param id
* @return
* @throws Exception
*/
@RequestMapping(value ="/delete", method = RequestMethod.POST)
@ResponseBody
public AjaxJson delete(String id) throws Exception {
    AjaxJson j = new AjaxJson();
    goodsTypeService.delete(id);
    j.setMsg("删除成功");
    return j;
}

    @RequestMapping(value ="/select", method = RequestMethod.POST)
    @ResponseBody
    public List<GoodsType> findAll() throws Exception {
        List<GoodsType> goodsTypes = goodsTypeService.findAll();
        return goodsTypes;
    }

    /**
     * 根据ID查询名称
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getGoodsTypeNameById")
    @ResponseBody
    public AjaxJson getGoodsTypeNameById(String id) throws Exception {
        AjaxJson j = new AjaxJson();
        GoodsType goodsType = goodsTypeService.findById(id);
        j.setObj(goodsType.getTitle());
        return j;
    }

}